package com.Controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.Entity.User;
import com.Mapper.UserMapper;

@Controller
@RequestMapping("/user")
public class UserController {
	private static Logger log=Logger.getLogger(UserController.class);
	
	@Autowired
	private UserMapper userMapper;
	
	
	/*
	 * 注册方法
	 * */
	@PostMapping("/register")
	@ResponseBody
	public User insert(@RequestBody User user) {
		log.info("进入注册方法");
		String username=user.getName();
		String password=user.getPassword();
		/*进行MD5加密*/
        String md5Password = DigestUtils.md5DigestAsHex(password.getBytes());
        userMapper.insert(username,md5Password);
		return user;
	}
	
	/*
	 * 登录方法
	 * */
	@PostMapping("/login")
	@ResponseBody
	public User login(@RequestBody User user) {
		/*入参*/
		String usernameIn=user.getName();
		String passwordIn=user.getPassword();
		/*进行MD5加密*/
		String passwordInMD5=DigestUtils.md5DigestAsHex(passwordIn.getBytes());
        String password = userMapper.selectByName(usernameIn);
        /*登录判断*/
        if(password.equals(passwordInMD5)) {
        	log.info("登陆成功");
        }else {
        	log.info("密码错误");
        }
		return user;
	}

}
